package com.holystone.orca.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.holystone.orca.common.JSONMapper;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.common.SystemConstant;
import com.holystone.orca.sys.mybatis.model.SysCompany;
import com.holystone.orca.sys.service.ISysCompanyService;
@Controller
@RequestMapping("/sys/company_management")
public class SysCompanyController {
	private static Logger logger = Logger.getLogger(SysCompanyController.class);
	private String baseUrl = "sys/company_management/";
	
	@Resource(name="sysCompanyService")
	private ISysCompanyService sysCompanyService;
	
    @RequestMapping("/menuCompanyList")
    public ModelAndView initCompanyList(ModelMap modelmap){
    	ModelAndView model = new ModelAndView(baseUrl+"company_management");
        return model;
    }
    //初始化页面
    @ResponseBody
    @RequestMapping("/pagingCompanyList")
    public ModelMap pagingCompanyList(String  iPageNo, String iPageSize,String cmpName){
    	int pageindex = iPageNo!=null?Integer.parseInt(iPageNo):SystemConstant.default_pageNum;
        int pagesize = iPageSize!=null?Integer.parseInt(iPageSize):SystemConstant.default_pageSize;
        PageData pageData = new PageData();
        try{
            pageData = sysCompanyService.pagingSysCompany(pageindex, pagesize,"cmp_name",cmpName);
        }catch(Exception e){
            logger.error(e,e);
        }
        return JSONMapper.createReturnMap(null, pageData, null);
    }
    
    
    //初始化添加页面
    @RequestMapping("/initAddSysCompany")
    public ModelAndView initAddSysCompany(){
    	ModelAndView model = new ModelAndView(baseUrl+"add_company");
        return model;
    }
    
    //初始化编辑页面
    @RequestMapping("/initEditSysCompany")
    public ModelAndView initEditSysCompany(int cmpId,ModelMap modelMap){
    	List<SysCompany> list=new ArrayList<SysCompany>();
    	try {
			list=sysCompanyService.selectUpdateCompany(cmpId);
		} catch (Exception e) {
			logger.error(e,e);
		}
    	modelMap.put("sysCompanyList", list);
    	ModelAndView model = new ModelAndView(baseUrl+"edit_company");
        return model;
    }
    
    //编辑公司信息
    @ResponseBody
    @RequestMapping("/updateSysCompany")
    public ModelMap updateSysCompany(SysCompany sysCompany){
    	boolean flag=false;
    	try {
			flag=sysCompanyService.updateCompany(sysCompany);
		} catch (Exception e) {
			 logger.error(e,e); 
		}
    	return JSONMapper.createReturnMap(null, null, flag);
    }
    
    //添加公司
    @ResponseBody
    @RequestMapping("/insertSysCompany")
    public ModelMap insertSysCompany(SysCompany sysCompany){
    	boolean flag=false;
    	try {
			flag=sysCompanyService.insertCompany(sysCompany);
		} catch (Exception e) {
			 logger.error(e,e);
		}
    	return JSONMapper.createReturnMap(null, null, flag);
    }
    
    //删除选中的公司
    @ResponseBody
    @RequestMapping("/deleteCompany")
    public ModelMap deleteCompany(int lookupCode){
    	boolean flag=false;
    	try {
			flag=sysCompanyService.deleteCompany(lookupCode);
		} catch (Exception e) {
			 logger.error(e,e);
		}
    	
    	 return JSONMapper.createReturnMap(null, null, flag);
    }
    
    //多项删除
    @ResponseBody
    @RequestMapping("/deleteSelectSysCompany")
    public ModelMap deleteSelectSysCompany(String checkedDel){
    	boolean flag=false;
    	checkedDel=checkedDel.substring(0, checkedDel.length() - 1);
    	String[] arry=null;
    	arry=checkedDel.split(",");
    	List<Integer> list=new ArrayList<Integer>();
    	for(int i=0;i<arry.length;i++){
    		list.add(Integer.parseInt(arry[i]));
    	}
    	try {
    		flag=sysCompanyService.delSelectCompany(list);
    	} catch (Exception e) {
    		logger.error(e,e);
    	}
    	return JSONMapper.createReturnMap(null, null, flag);
    }
    
    
}
